import os
import unittest
from libs.config.driver_config import DriverConfig


class TestCase(unittest.TestCase):
    """继承了unittest.TestCase,作为所有测试用例的基类"""

    project_dir = os.path.abspath('..')  # 项目目录
    deviceinfo = DriverConfig().get_desired_caps("MUMU")  # 初始化设备信息
    DriverConfig().print_device_info(deviceinfo)


def screenshot(func):
    """测试用例装饰器，执行失败后自动截图"""
    def wrapper(self, *args, **kwargs):
        try:
            func(self, *args, **kwargs)
        except Exception as e:
            self.driver.switch_to.context("NATIVE_APP")
            dir = "result/screenshot/" + str(func.__name__) + ".png"  # 用case名当做截图名称
            path = os.path.join(self.project_dir, dir)
            self.driver.save_screenshot(path)
            raise e
    return wrapper
